package com.action;

import java.util.List;
import java.util.Map;

import com.hdb.DB;
import com.hdb.TGood;
import com.hdb.TUser;
import com.hdb.dao.Types;
import com.util.Cart;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@ParentPackage("default")
@Namespace("/")
@Results({
        @Result(location = "/view/index.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "noticeAll", location = "/view/notice/noticeAll.jsp")
})
public class IndexAction extends BasicAction {
    private DB db;
    private int userId;
    private String userName;
    private String userPw;
    private String userRealName;
    private int userSex;
    private String userTel;
    private String userEmail;
    private String userQq;
    private int page;
    private String message;
    private String path;
    @SuppressWarnings("unchecked")
    @Action("index")
    public String index() {
        Map request = (Map) ServletActionContext.getContext().get("request");
        //不特价
        String hql = "from TGood where goodState=1 and goodType=? order by goodId desc";
        List goodNormalList =
                db.goodDAO.getSession()
                        .createQuery(hql)
                        .setParameter(0, Types.good.Normal)
                        .setMaxResults(12)
                        .list();
        request.put("goodNormalList", goodNormalList);

        //排行
        hql = "from TGood where goodState=1 order by goodSell desc";
        List<TGood> goodList =
                db.goodDAO.getSession().createQuery(hql)
                        .setMaxResults(12)
                        .list();
        request.put("goodList", goodList);


/*       //公告
        hql = "from TNotice order by noticeTop,noticeDate desc";
        List<TGood> noticeList =
                db.goodDAO.getSession().createQuery(hql)
                        .setMaxResults(6)
                        .list();
        request.put("noticeList", noticeList);*/
        return ActionSupport.SUCCESS;
    }

    /*
     * 用户查看发布的公告
     */
    @Action("noticeAll")
    public String noticeAll() {
        int[] p = getP();
        List noticeList = db.noticeByTop(p[0], p[1]);
        getRequest().put("noticeList", noticeList);
        getPLink(noticeList.size());
        return "noticeAll";
    }


    @Action("userReg")
    public String userReg() {
        TUser user = getUser();
        if (null != user) {
            setMessage("您已经登录");
            setPath("index.action");
            return "succeed";
        }
        if (db.userDAO.findOne("userName", userName) != null) {
            setMessage("注册失败,用户名已被占用");
            setPath("view/user/userReg.jsp");
            return "succeed";
        }
        if (userPw.trim().length() < 6) {
            setMessage("密码至少6位");
            setPath("view/user/userReg.jsp");
            return "succeed";
        }
        user = new TUser();
        user.setUserName(userName);
        user.setUserPw(userPw);
        user.setUserTel(userTel);
        user.setUserRealName(userRealName);
        user.setUserEmail(userEmail);
        user.setUserSex(userSex);
        user.setUserQq(userQq);
        if (db.userDAO.save(user)) {
            setMessage("注册成功");
            setUser(user);
            setCart(new Cart());
            setPath("index.action");
        } else {
            setMessage("注册失败");
            setPath("view/user/userReg.jsp");
        }
        return "succeed";
    }

    @Action("userLogin")
    public String userLogin() {
        TUser user = db.findUser(userName, userPw);
        if (user == null) {
            this.setMessage("用户名或密码错误");
            this.setPath("view/user/userLogin.jsp");
        } else {
            setUser(user);
            setCart(new Cart());
            this.setPath("index.action");
        }
        return "succeed";
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPw() {
        return userPw;
    }

    public void setUserPw(String userPw) {
        this.userPw = userPw;
    }

    public String getUserRealName() {
        return userRealName;
    }

    public void setUserRealName(String userRealName) {
        this.userRealName = userRealName;
    }

    public int getUserSex() {
        return userSex;
    }

    public void setUserSex(int userSex) {
        this.userSex = userSex;
    }

    public String getUserTel() {
        return userTel;
    }

    public void setUserTel(String userTel) {
        this.userTel = userTel;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserQq() {
        return userQq;
    }

    public void setUserQq(String userQq) {
        this.userQq = userQq;
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public void setPage(int page) {
        this.page = page;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
